#include "user.h"

/** 用户单链表初始化  即创建头节点
    u_list* init_list(void)
    返回 user
 */
u_list* init_list(void)
{
    u_list* user=(u_list*)malloc(sizeof(u_list));
    if (NULL == user )
    {
        ERRLOG("user_List 创建失败\n");
    }
    user->next=NULL;
    user->len=0;
    return user;   
}
/** 用户单链表赋初值  即随机生成账户信息
    void init_list_random(u_list* user)
    参数 u_list* user
    返回 void 
 */

void init_list_random(int number)
{
    char user_name[][20] =
        {"龔美玲", "柳嘉杰", "侯柏林", "文伟豪", "蔡安娜", "阮欣怡",
         "管敏琪", "柯翔宇", "萬泽霖", "鍾子祺", "馬宇鑫", "熊嘉文",
         "葉紫彤", "韋志成", "賈云云", "何海东", "已复制", "陶佳文",
         "汪晓倩", "章文泽", "方文雅", "吳春艳", "丁俊轩", "佘思敏",
         "湯小梅", "熊丽霞", "梁玉萍", "梁玉萍", "易小龙", "蔡小丽",
         "刘备", "关羽", "张飞", "诸葛亮", "赵云", "马超",
         "黄忠", "魏延", "姜维", "庞统", "法正", "曹操",
         "曹丕", "曹植", "司马懿", "郭嘉", "荀彧", "夏侯惇",
         "夏侯渊", "张辽", "徐晃", "张郃", "许褚", "典韦",
         "孙权", "周瑜", "陆逊", "吕蒙", "鲁肃", "黄盖",
         "太史慈", "甘宁", "周泰", "凌统", "程普", "韩当",
         "袁绍", "袁术", "刘表", "刘璋", "董卓", "吕布",
         "貂蝉", "陈宫", "高顺", "张角", "张宝", "张梁",
         "华雄", "颜良", "文丑", "公孙瓒", "孟获", "祝融", "于吉",
         "云飞扬", "林逸风", "苏瑾瑜", "李晨曦", "赵雨萱", "周逸尘",
          "吴韵诗", "陈星河", "孙梦琪", "高云翔",
         "郑思远", "王梓轩", "郭晓月", "萧逸才", "何静雅", "唐婉如",
          "秦浩然", "罗文轩", "宋梓萱", "许悠然",
         "陆瑾萱", "傅清扬", "程晓曦", "叶倾城", "方天翼", "余梦琪",
          "杜若溪", "韩晨曦", "柳如烟", "江雨婷",
         "蔡雨泽", "谢婉清", "邹天翼", "谭梦琪", "熊浩然", "黎晨曦",
          "白子墨", "毛思远", "颜如玉", "卢逸飞",
         "梅雪舞", "段逸风", "卫无双", "郝梦琪", "毕婉如", "雷震天",
          "樊星辰", "于瑾萱", "石破天", "金雨泽",
         "贺子轩", "项天歌", "梁晓月", "董晨曦", "章静雅", "蒋云翔", 
         "沈梦琪", "韩韵诗", "任逸尘", "田晓曦",
         "杜星河", "鲁婉清", "卫凌霄", "崔天翼", "龙逸风", "陶瑾萱",
          "范思远", "秦雨萱", "岳晨曦", "齐婉如",
         "邵梦琪", "袁浩然", "叶雨泽", "常逸尘", "程天翼", "余星河",
          "薛婉清", "雷瑾萱", "孔思远", "方晨曦",
         "戴雨婷", "林天歌", "欧阳雪", "司马云", "上官月", "独孤风", 
         "百里寒", "东方明", "南宫瑾", "西门雪",
         "北堂轩", "慕容琳", "尉迟飞", "宇文轩", "长孙煜", "鲜于晨",
          "拓跋梦", "贺兰晴", "完颜瑾", "皇甫逸","左慈"};
    char passwd[] = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '=', '_',
        '[', ']', '{', '}', '|', '\\', ':', ';', '"', '"', '<', '>', ',', '.',
        '?', '/', '~', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    char phone_Network_id[][4]=
    {
        "134","135","136","137","138","139","150","151",
        "152","158","159","157","182","187","188",
        "130","131","132","155","156","182","185",
        "186","133","153","180","189"
    };

   
    int phone_Network_id_size = sizeof(phone_Network_id) / sizeof(phone_Network_id[0]);
    int user_name_size=sizeof(user_name)/sizeof(user_name[0]);
    int passwd_size=sizeof(passwd);
  
    char  temp_ch[0];
    user_t user_data ;
  
    for (int  i = 0; i <number; i++)
    {
        //姓名
        strcpy(user_data.name,user_name[rand()%user_name_size]) ;  
        //性别
        strcpy(user_data.sex,rand()%2?"女":"男" );
        //唯一标识id 
        for (size_t i = 0; i <10 ; i++)
        {
            temp_ch[0]=rand()%10+'0';
            user_data.account[i]=temp_ch[0];
        }
        //密码
        for (size_t i = 0; i < 10 ; i++)
        {
            user_data.passwd[i]=passwd[rand()%passwd_size];
        }
        //电话号码
        strcpy(user_data.phone,phone_Network_id[rand()%phone_Network_id_size]);
        for (size_t i = 0; i < 8; i++)
        {
            temp_ch[0]=rand()%10+'0';
            user_data.phone[i+3]=temp_ch[0];
        }
    
        //用户等级
        user_data.status=rand()%10;
   
  
       //插入到单链表   user_data

        pos_insert_LinkList(user_data,1);
        printf("生成一条数据\n");
           // printf("account=%s\t IDcard=%s\t name=%s\t passwd=%s\t phone=%s\t sex=%s\t status=%c\t value=%d\n",
        // user_data.account,user_data.IDcard,user_data.name,user_data.passwd,user_data.phone,
        // user_data.sex,user_data.status,user_data.value);
    }


}



/*'   此插入方法为任意插入 带查重  
        单链表插入之第一步  
        插入之前判断此数据是否重复   即账号唯一 
             
*/
int pos_insert_LinkList(user_t data,int pos)
{


    if (user==NULL)
    {
        ERRLOG("插入的链表为NULL\n");
        return -1;
    } 
    u_node* node_Link=(u_node*)malloc(sizeof(u_node));
    if (NULL==node_Link)
    {
        ERRLOG("新节点为NULL，创建失败\n");
        return -1;
    
    }  
    node_Link->user_data=data;
    node_Link->next=NULL;
    //遍历是否账号重复   
    u_node *temp = user->next;
    for (size_t j = 0; NULL != temp; j++)
    {
        if (0 == strcmp(temp->user_data.account, data.account))
        {
            ERRLOG("账号id与已有id重复  请勿重复添加\n");
            return -1;
        }
        temp = temp->next;
    }

    //寻找插入位置
    temp=user->next;   
    if (pos==1)
    {
        node_Link->next=user->next;
        user->next=node_Link;
        user->len++;
        return 1;
    }
 
    for (size_t i = 1; i < pos-1  &&  temp!=NULL ; i++)
    {
        temp=temp->next;      
    }
 
    if (temp==NULL)
    {
       printf("插入位置有误\n");
       return -1;
    }
     
    node_Link->next=temp->next;
    temp->next=node_Link;
    user->len++;
 
    return 1;

}

user_t find_userinfo(user_t info)
{
    if (user == NULL)
    {
        ERRLOG("这个链表为NULL\n");
        info.status = -1;
        return info;
    }
    if (user->next == NULL)
    {
        ERRLOG("这个链长度为0\n");
          info.status = -1;
    return info;
    }
    u_node *ptr1;
    ptr1 = user->next;

    for (size_t i = 1; ptr1 != NULL; i++)
    {
        if (0 == strcmp(ptr1->user_data.account, info.account))
        {
            if (0 == strcmp(ptr1->user_data.passwd, info.passwd))
            {
                return ptr1->user_data;
            }
            break;
           
        }

        ptr1 = ptr1->next;
    }
    info.status = -1;
    return info;

}


user_t find_friend_userinfo(user_t info)
{
    if (user == NULL)
    {
        ERRLOG("这个链表为NULL\n");
        info.status = -1;
        return info;
    }
    if (user->next == NULL)
    {
        ERRLOG("这个链长度为0\n");
          info.status = -1;
    return info;
    }
    u_node *ptr1;
    ptr1 = user->next;

    for (size_t i = 1; ptr1 != NULL; i++)
    {
        if (0 == strcmp(ptr1->user_data.name, info.name))
        {
           
            return ptr1->user_data;
            
       
           
        }

        ptr1 = ptr1->next;
    }
    info.status = -1;
    return info;

}


user_t find_userinfo_passwd(user_t info)
{
    if (user == NULL)
    {
        ERRLOG("这个链表为NULL\n");
        info.status = -1;
        return info;
    }
    if (user->next == NULL)
    {
        ERRLOG("这个链长度为0\n");
          info.status = -1;
    return info;
    }
    u_node *ptr1;
    ptr1 = user->next;

    for (size_t i = 1; ptr1 != NULL; i++)
    {
        if (0 == strcmp(ptr1->user_data.account, info.account))
        {
            return ptr1->user_data;
           
           
        }

        ptr1 = ptr1->next;
    }
    info.status = -1;
    return info;

}

// 任意位置删除  
void account_del_LinkList(char* account)
{
    if (user==NULL)
    {
        ERRLOG("这个链表为NULL\n");
        return ;
    }
    if (user->next==NULL)
    {
        ERRLOG("这个链长度为0\n");
        return ;
    }
    u_node* ptr1, *ptr2;
    ptr1=user->next;

    for (size_t i = 1;   ptr1!=NULL ; i++)
    {
        if (0 == strcmp(ptr1->user_data.account , account ) )
        {
            
            ptr2=ptr1->next;
            user->next=ptr2;
            free(ptr1);
            user->len--;
            return;
        }
        
        ptr1=ptr1->next;
    }
    if (ptr1==NULL)
    {
        ERRLOG("需要删除的账号不存在\n");
   
    }
    
    return;

}


//清除所有账号
void clear_link_list(void)
{
     if (user==NULL)
    {
        ERRLOG("这个链表为NULL\n");
        return ;
    }
    if (user->next==NULL)
    {
        ERRLOG("这个链长度为0\n");
        return ;
    }
    u_node* ptr1=NULL;//指向要删除的元素
    for (size_t i = 0; user->next!=NULL; i++)
    {
        ptr1=user->next;
        user->next=ptr1->next;
        free(ptr1);
        user->len--;
    }
    
   
}





void Print_user_data(user_t user_data)
{
    printf("account=%s\t name=%s\t passwd=%s\t phone=%s\t sex=%s\t status=%c\t \n",
        user_data.account,user_data.name,user_data.passwd,user_data.phone,
        user_data.sex,user_data.status);

}


/*                                              好友列表                                        */


friend_list* fri_init_list(void)
{
    friend_list* user=(friend_list*)malloc(sizeof(friend_list));
    if (NULL == user )
    {
        ERRLOG("friend_list 创建失败\n");
    }
    user->next=NULL;
    user->len=0;
    return user;   
}
/*  打印好友列表 */
void friend_Print(friend_list* friend )
{
    friend_node *ptr=friend->next;
    for (size_t i = 0; i < friend->len; i++)
    {
        printf( "姓名:%s\t好友列表:%s\n",ptr->user_data.user_name,ptr->user_data.firend);
        ptr=ptr->next;
    }

}

/*'   此插入方法为任意插入 带查重  
        单链表插入之第一步  
        插入之前判断此数据是否重复   即账号唯一 
             
*/
int fri_pos_insert_LinkList(FriendList data,int pos  ,friend_list* friend )
{
    printf("\n--1-----------------------\n");

    if (friend==NULL)
    {
        ERRLOG("插入的链表为NULL\n");
        return -1;
    } 
    friend_node* node_Link=(friend_node*)malloc(sizeof(friend_node));
    if (NULL==node_Link)
    {
        ERRLOG("新节点为NULL，创建失败\n");
        return -1;
    
    }  
    node_Link->user_data=data;
    node_Link->next=NULL;
    //遍历是否账号重复   
    friend_node *temp = friend->next;

    for (size_t j = 0; NULL != temp; j++)
    {
        if (0 == strcmp(temp->user_data.user_name, data.user_name))
        {
            ERRLOG("这个账号只有一个好友列表 且已经存在\n");
            return -1;
        }
        temp = temp->next;
    }
printf("\n-2------------------------\n");
    //寻找插入位置
    temp=friend->next;   
    if (pos==1)
    {
        node_Link->next=friend->next;
        friend->next=node_Link;
        friend->len++;
        return 1;
    }
 
    for (size_t i = 1; i < pos-1  &&  temp!=NULL ; i++)
    {
        temp=temp->next;      
    }
 
    if (temp==NULL)
    {
       printf("friend插入位置有误\n");
       return -1;
    }
     printf("\n---3----------------------\n");
    node_Link->next=temp->next;
    temp->next=node_Link;
    friend->len++;
    printf("插入成功\n");
    printf("\n-------4------------------\n");
    return 1;

}

FriendList fri_find_userinfo(FriendList info , friend_list* friend )
{
    if (friend == NULL)
    {
        ERRLOG("这个链表为NULL\n");
        
        return info;
    }
    if (friend->next == NULL)
    {
        ERRLOG("这个链长度为0\n");
        return info;
    }
    friend_node *ptr1;
    ptr1 = friend->next;
    printf("info.user_name :%s\n",info.user_name);
    for (size_t i = 1 ; ptr1 != NULL ; i++)
    {
        if (0 == strcmp(ptr1->user_data.user_name ,info.user_name))
        {
            printf("user_data :%s|%s\n",ptr1->user_data.user_name,ptr1->user_data.firend);
            return ptr1->user_data;
           
        }
        printf("ptr1->user_data.user_name :%s\n",ptr1->user_data.user_name);
       
        ptr1 = ptr1->next;
    }
    ERRLOG("这个账号的好友为0 \n");
    return info;

}
/*    修改好友列表    */
int fri_alter_userinfo(FriendList info , friend_list* friend )
{
  if (friend == NULL)
    {
        ERRLOG("这个链表为NULL\n");
        
        return 0;
    }
    if (friend->next == NULL)
    {
        ERRLOG("这个链长度为0\n");
        return 0;
    }
    friend_node *ptr1;
    ptr1 = friend->next;
    printf("info.user_name :%s\n",info.user_name);
    for (size_t i = 1 ; ptr1 != NULL ; i++)
    {
        if (0 == strcmp(ptr1->user_data.user_name ,info.user_name))
        {

            //修改好友列表
            printf("%s | %s\n",ptr1->user_data.user_name ,ptr1->user_data.firend);
            strcat(ptr1->user_data.firend ,  info.firend  );
            ERRLOG("修改成功");
            printf("%s | %s\n",ptr1->user_data.user_name ,ptr1->user_data.firend);
            return 1;
           
        }
        //printf("ptr1->user_data.user_name :%s\n",ptr1->user_data.user_name);
       
        ptr1 = ptr1->next;
    }
    ERRLOG("修改失败 \n");
    return 0;
}
void removeAllSubstrings(char *src, const char *remove) {  
    char *p = src;  
    char *start;  
  
    while ((p = strstr(p, remove)) != NULL) {  
        start = p;  
        p += strlen(remove); // 移动到子串之后  
  
        // 将子串后面的字符前移，覆盖子串  
        memmove(start, p, strlen(p) + 1);  
  
        // 注意：此时不要增加p，因为src已经被修改了  
        // 但我们需要确保下一次搜索从当前位置之后开始  
        // 由于我们已经将子串后面的内容前移，所以start（即原来的p）现在指向了新的搜索起点  
    }  
}
int fri_del_userinfo(FriendList info , friend_list* friend )
{
    if (friend == NULL)
    {
        ERRLOG("这个链表为NULL\n");
        
        return 0;
    }
    if (friend->next == NULL)
    {
        ERRLOG("这个链长度为0\n");
        return 0;
    }
    friend_node *ptr1;
    ptr1 = friend->next;
    printf("info.user_name :%s\n",info.user_name);

    for (size_t i = 1 ; ptr1 != NULL ; i++)
    {
        if (0 == strcmp(ptr1->user_data.user_name ,info.user_name))
        {

            //修改好友列表
            printf("%s | %s\n", ptr1->user_data.user_name, ptr1->user_data.firend);
            removeAllSubstrings(ptr1->user_data.firend, info.firend);
            printf("%s | %s\n", ptr1->user_data.user_name, ptr1->user_data.firend);
            ERRLOG("修改成功");
            return 1;
           
        }
        //printf("ptr1->user_data.user_name :%s\n",ptr1->user_data.user_name);
       
        ptr1 = ptr1->next;
    }
    ERRLOG("修改失败 \n");
    return 0;
    
}
// 任意位置删除  
void fri_account_del_LinkList(FriendList info , friend_list* friend )
{
    if (friend==NULL)
    {
        ERRLOG("这个链表为NULL\n");
        return ;
    }
    if (friend->next==NULL)
    {
        ERRLOG("这个链长度为0\n");
        return ;
    }
    friend_node* ptr1, *ptr2;
    ptr1=friend->next;

    for (size_t i = 1;   ptr1!=NULL ; i++)
    {
        if (0 == strcmp(ptr1->user_data.user_name , info.user_name ) )
        {
            
            ptr2=ptr1->next;
            friend->next=ptr2;
            free(ptr1);
            friend->len--;
            return;
        }
        
        ptr1=ptr1->next;
    }
    if (ptr1==NULL)
    {
        ERRLOG("需要删除的账号不存在\n");
   
    }
    
    return;

}


//清除所有账号
void fri_clear_link_list(friend_list* friend)
{
     if (friend==NULL)
    {
        ERRLOG("这个链表为NULL\n");
        return ;
    }
    if (friend->next==NULL)
    {
        ERRLOG("这个链长度为0\n");
        return ;
    }
    friend_node* ptr1=NULL;//指向要删除的元素
    for (size_t i = 0; friend->next!=NULL; i++)
    {
        ptr1=friend->next;
        friend->next=ptr1->next;
        free(ptr1);
        friend->len--;
    }
    
   
}

